﻿using DotNetMVC.DTO;
using DotNetMVC.IService;
using DotNetMVC.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetMVC.Service
{
    public partial class FunctionalModuleService : BaseService<FunctionalModule>, IFunctionalModuleService
    {
        public bool ExistByFunCode(string funCode)
        {
            return GetAll().Any(e => e.FunCode == funCode); ;
        }

        public FunctionalModuleDTO[] GetFunctionalModules()
        {
            return GetAll().ToList().Select(p => ToDTO(p)).ToArray();
        }

        public FunctionalModuleDTO[] GetEnabledFunctionalModules()
        {
            return GetAll().Where(e=>e.IsEnabled==true).ToList().Select(p => ToDTO(p)).ToArray();
        }

        public FunctionalModuleDTO GetDTOById(long id)
        {
            FunctionalModule entity = GetById(id);
            if (entity == null)
            {
                return null;
                //throw new Exception("获取不到功能分类(Id:" + id + ")信息");
            }
            return ToDTO(entity);
        }

        public FunctionalModuleDTO GetDTOByFunCode(string funCode)
        {
            FunctionalModule entity = GetAll().SingleOrDefault(e=>e.FunCode== funCode);
            if (entity == null)
            {
                return null;
                //throw new Exception("获取不到功能分类(Id:" + id + ")信息");
            }
            return ToDTO(entity);
        }

        private FunctionalModuleDTO ToDTO(FunctionalModule entity)
        {
            FunctionalModuleDTO dto = new FunctionalModuleDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.FunName = entity.FunName;
            dto.FunCode = entity.FunCode;
            dto.CtrlUrl = entity.CtrlUrl;
            dto.ViewHtmlTemplate = entity.ViewHtmlTemplate;
            dto.ListHtmlTemplate = entity.ListHtmlTemplate;
            dto.Sort = entity.Sort;
            dto.IsEnabled = entity.IsEnabled;
            dto.Remark = entity.Remark;
            return dto;
        }


        public void Add(string funName, string funCode, string ctrlUrl, string viewHtmlTemplate, string listHtmlTemplate, int sort, bool isEnabled, string remark)
        {
            FunctionalModule entity = new FunctionalModule();
            entity.FunName = funName;
            entity.FunCode = funCode;
            entity.CtrlUrl = ctrlUrl;
            entity.ViewHtmlTemplate = viewHtmlTemplate;
            entity.ListHtmlTemplate = listHtmlTemplate;
            entity.Sort = sort;
            entity.IsEnabled = isEnabled;
            entity.Remark = remark;
            Add(entity);
        }

        public void Update(long id, string funName, string funCode, string ctrlUrl, string viewHtmlTemplate, string listHtmlTemplate, int sort, bool isEnabled, string remark)
        {
            FunctionalModule entity = GetById(id);
            if (entity == null)
            {
                throw new Exception("获取不到功能分类(Id:" + id + ")信息");
            }
            entity.FunName = funName;
            entity.FunCode = funCode;
            entity.CtrlUrl = ctrlUrl;
            entity.ViewHtmlTemplate = viewHtmlTemplate;
            entity.ListHtmlTemplate = listHtmlTemplate;
            entity.Sort = sort;
            entity.IsEnabled = isEnabled;
            entity.Remark = remark;
            Update(entity);
        }
    }
}
